package vn.nt.healthcare.server.ws.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.nt.healthcare.server.ws.model.Customer;
import vn.nt.healthcare.server.ws.model.CustomerInfo;
import vn.nt.healthcare.server.ws.model.Pregnant;
import vn.nt.healthcare.server.ws.service.PregnantService;

@Controller
public class PregnantController {

	@Autowired
	private PregnantService pregnantService;

	@RequestMapping(value = "/pergnant/detail", method = RequestMethod.GET)
	@ResponseBody
	public CustomerInfo findPregnantById(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "id") int id) {
		return pregnantService.findPregnantById(companyId, id);
	}

	@RequestMapping(value = "/pregnant/listall", method = RequestMethod.GET)
	@ResponseBody
	public List<CustomerInfo> findAll(HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId) {
		return pregnantService.findAll(companyId);
	}

	@RequestMapping(value = "/pregnant/add", method = RequestMethod.PUT)
	@ResponseBody
	public String addPregnant(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "os") String os,
			@RequestParam(value = "token") String token,
			@RequestBody CustomerInfo customer) {
		Pregnant pregnant = parserInfo(customer);
		boolean result = pregnantService.savePregnantAndPush(companyId,
				pregnant, os, token);
		if (result) {
			return "Update info successfully";
		} else {
			return "Update info failed";
		}
	}

	@RequestMapping(value = "/pregnant/delete", method = RequestMethod.DELETE)
	@ResponseBody
	public String delete(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "id") int id) {
		boolean result = pregnantService.removePregnant(companyId, id);
		if (result) {
			return "Delete information of this pregnant";
		} else
			return "";
	}

	@RequestMapping(value = "/pregnant/save", method = RequestMethod.PUT)
	@ResponseBody
	public String addPregnant(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestBody CustomerInfo customer) {
		Pregnant pregnant = parserInfo(customer);
		boolean result = pregnantService.saveOrUpdatePregnant(companyId,
				pregnant);
		if (result) {
			return "Update info successfully";
		} else {
			return "Update info failed";
		}
	}

	private Pregnant parserInfo(CustomerInfo customer) {
		Pregnant pregnant = new Pregnant();
		pregnant.setFemale_date(customer.getFemale_date());
		pregnant.setPeriod(customer.getPeriod());
		pregnant.setState(customer.getState());
		pregnant.setWeight(customer.getWeight());
		pregnant.setHeight(customer.getHeight());
		pregnant.setTypeBlood(customer.getTypeBlood());
		Customer customer_ = new Customer();
		customer_.setAge(customer.getAge());
		customer_.setBirthday(customer.getBirthday());
		customer_.setPhone(customer.getPhone());
		customer_.setEmail(customer.getEmail());
		customer_.setFirstname(customer.getFirstname());
		customer_.setLastname(customer.getLastname());
		customer_.setNr(customer.getNr());
		customer_.setStreet(customer.getStreet());
		customer_.setCity(customer.getCity());
		pregnant.setCustomer(customer_);
		return pregnant;
	}
}
